﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Filter;
using COFE.Core.Utils;
using COFE.Core;

namespace COFE.Data.Filter
{
    [EntryFilter("expire")]
    public class DBExpireEntryFilter : EntryFilterBase
    {
        #region Consturctor

        public DBExpireEntryFilter(DBExpiredType expiredType)
            : this(expiredType.ToString())
        {

        }

        public DBExpireEntryFilter(string type, string id = "expire")
            : base(type, id)
        {
            if (type.ToLower() == "prop")
                _expiredType = DBExpiredType.Property;
            else
                if (!(EnumUtils<DBExpiredType>.EnumTryParse(type,
                    out _expiredType)))
                    _expiredType = DBExpiredType.None;
        }

        #endregion

        #region Methods

        public override MatchType matches(
            IInternalFileSystemInfoExA entry, params IOptionFilter[] options)
        {
            switch (_expiredType)
            {
                case DBExpiredType.Property:
                    var propTime = entry.GetProperty(DatabaseProperties.PropertyRefreshTime)
                        .ValueAsUtcDateTime;
                    if (DateTime.UtcNow.Subtract(propTime) > COFEDBShared.TTLPropertyValues)
                        return MatchType.MatchThisEntry;
                    break;
                case DBExpiredType.List:
                    if (entry is IInternalDirectoryInfoExA)
                    {
                        var listTIme = entry.GetProperty(DatabaseProperties.LastListTime)
                            .ValueAsUtcDateTime;
                        if (DateTime.UtcNow.Subtract(listTIme) > COFEDBShared.TTLEntryList)
                            return MatchType.MatchThisEntry;
                    }

                    break;
            }
            return MatchType.None;

        }

        #endregion

        #region Data

        DBExpiredType _expiredType;

        #endregion

        #region Public Properties

        public DBExpiredType ExpiredType { get { return _expiredType; } }

        #endregion
    }
}
